package com.chap17.completablefuture;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * <code>Demo21</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-05-11 10:38
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
@Slf4j
public class Demo21 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        FutureTask<String> ft2 = new FutureTask<>(new T2Task());
        FutureTask<String> ft1 = new FutureTask<>(new T1Task(ft2));

        Thread t1 = new Thread(ft1);
        t1.start();

        Thread t2 = new Thread(ft2);
        t2.start();


        log.debug(ft1.get());
    }


    //需要执行的任务：洗水壶 烧开水 泡茶
    static class T1Task implements Callable<String>{
        FutureTask<String> ft2;

        public T1Task(FutureTask<String> ft2) {
            this.ft2 = ft2;
        }

        @Override
        public String call() throws Exception {
            log.debug("T1:洗水壶");
            TimeUnit.SECONDS.sleep(1);

            log.debug("T1:烧开水");
            TimeUnit.SECONDS.sleep(15);

            //拿茶叶
            String s = ft2.get();
            log.debug("T1:拿到茶叶：{}",s);

            log.debug("T1:泡茶");
            return "上茶："+s;
        }
    }

    //洗茶壶 洗茶杯 拿茶叶
    static class T2Task implements Callable<String>{

        @Override
        public String call() throws Exception {
            log.debug("T2:洗茶壶");
            TimeUnit.SECONDS.sleep(1);

            log.debug("T2:洗茶杯");
            TimeUnit.SECONDS.sleep(2);

            log.debug("T2:拿茶叶");
            TimeUnit.SECONDS.sleep(1);
            return "龙井";
        }
    }
}
